//
//  String+Extenison.swift
//  CosClub
//
//  Created by 沙睿 on 2020/4/26.
//  Copyright © 2020 ios. All rights reserved.
//

import Foundation
import UIKit
import CommonCrypto


extension String {
    public var md5:String {
        let utf8 = cString(using: .utf8)
        var digest = [UInt8](repeating: 0, count: Int(CC_MD5_DIGEST_LENGTH))
        CC_MD5(utf8, CC_LONG(utf8!.count - 1), &digest)
        return digest.reduce("") { $0 + String(format:"%02X", $1)}
    }
}

extension String {
    
    public static func timeString(time: Int?) ->String? {
        guard let allTime = time else {
            return ""
        }

        var hours = 0
        var minutes = 0
        var seconds = 0
        var hoursText = ""
        var minutesText = ""
        var secondsText = ""
        
        hours = allTime / 3600
        hoursText = hours > 9 ? "\(hours)" : "0\(hours)"
        hoursText = hours == 0 ? "" : (hoursText + ":")
        
        minutes = allTime % 3600 / 60
        minutesText = minutes > 9 ? "\(minutes)" : "0\(minutes)"
        minutesText = minutesText + ":"
        seconds = allTime % 3600 % 60
        secondsText = seconds > 9 ? "\(seconds)" : "0\(seconds)"
        
        return hoursText + minutesText + secondsText
        
    }
    
    /// 和当前时间比较
    public static func compareCurrntTime(timeStamp:Int) ->String {
        if timeStamp == 0 {
            return ""
        }
        
        //计算出时间戳距离现在时间的一个秒数(..s)
        let interval:TimeInterval=TimeInterval.init(timeStamp)
        let date = Date(timeIntervalSince1970: interval)
        var timeInterval = date.timeIntervalSinceNow
        
        //得到的是一个负值 (加' - ' 得正以便后面计算)
        timeInterval = -timeInterval
        
        //根据时间差 做所对应的文字描述 (作为返回文字描述)
        var result:String
        
        //一分钟以内
        if interval < 60 {
            result = "刚刚"
            return result
        }else if Int(timeInterval/60) < 60 { //一小时以内
            return String.init(format:"%@分钟前",String(Int(timeInterval/60)))
        }else if Int((timeInterval/60)/60) < 24 { //一天以内
            return String.init(format:"%@小时前",String(Int((timeInterval/60)/60)))
        }else { //超过一天的
            return CusDateFormatter.shared.string(from: date as Date)
        }
    }
}

class CusDateFormatter: DateFormatter {
    public static let shared = { () -> DateFormatter in
       let data = DateFormatter()
        data.dateFormat="yyyy年MM月dd日 HH:mm"
        return data
    }()
}

extension String {
    /// 十六进制字符串颜色转为UIColor
    /// - Parameter alpha: 透明度
    /// - Returns: UIColor
    public func color(alpha: CGFloat = 1.0) -> UIColor {
        // 存储转换后的数值
        var red: UInt64 = 0, green: UInt64 = 0, blue: UInt64 = 0
        var hex = self
        // 如果传入的十六进制颜色有前缀，去掉前缀
        if hex.hasPrefix("0x") || hex.hasPrefix("0X") {
            hex = String(hex[hex.index(hex.startIndex, offsetBy: 2)...])
        } else if hex.hasPrefix("#") {
            hex = String(hex[hex.index(hex.startIndex, offsetBy: 1)...])
        }
        // 如果传入的字符数量不足6位按照后边都为0处理，当然你也可以进行其它操作
        if hex.count < 6 {
            for _ in 0..<6-hex.count {
                hex += "0"
            }
        }

        Scanner(string: String(hex[..<hex.index(hex.startIndex, offsetBy: 2)])).scanHexInt64(&red)
        // 绿
        Scanner(string: String(hex[hex.index(hex.startIndex, offsetBy: 2)..<hex.index(hex.startIndex, offsetBy: 4)])).scanHexInt64(&green)
        // 蓝
        Scanner(string: String(hex[hex.index(startIndex, offsetBy: 4)...])).scanHexInt64(&blue)

        return UIColor(red: CGFloat(red)/255.0, green: CGFloat(green)/255.0, blue: CGFloat(blue)/255.0, alpha: alpha)
    }
}
